﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DIProject.Dictionary
{
    public partial class DepartmentUsers : Form
    {
        public DepartmentUsers()
        {
            InitializeComponent();
        }

        private void DepartmentUsers_Load(object sender, EventArgs e)
        {           
            InitGrid();
        }
        private void InitGrid()
        {
            departmentUsersTableAdapter.Fill(dictionary.DepartmentUsers);

        }

        private void dgvDepUsers_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Int32 index = e.RowIndex;
            departmentUsersDataGridView.Rows[index].Selected = true;
            if (departmentUsersDataGridView.SelectedRows.Count < 1)
            {
                return;
            }
            DataRowView drv = departmentUsersDataGridView.SelectedRows[0].DataBoundItem as DataRowView;
            DataSets.Dictionary.DepartmentUsersRow row = drv.Row as DataSets.Dictionary.DepartmentUsersRow;
        }
        private bool _isNewUser = false;
        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedIndex = 1;
            mWTextBox.Text = FirstForm.Login;
            passwordBox.Visible = true;
            _isNewUser = true;
        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            DialogResult dRes = MessageBox.Show
            (
            "Удалить пользователя?",
            "Удаление",
            MessageBoxButtons.OKCancel,
            MessageBoxIcon.Question
            );
            if (dRes != DialogResult.OK)
                return;
        }
        /// <summary>
        /// Обновляем или создаем нового
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (!Validate())
                return;
            if (_isNewUser)
            {
                CreateUser();
                _isNewUser = false;
                InitGrid();
                tabControl1.SelectedIndex = 0;
            }
            else
            {
                departmentUsersBindingSource.EndEdit();
                departmentUsersTableAdapter.Update(dictionary.DepartmentUsers);
            }
        }
        private void CreateUser()
        {
            try
            {
                Int32 newUserId = DIProject.Data.Users.CreateDIUser(loginTextBox.Text, txbPassword.Text, firstNameTextBox.Text, secondNameTextBox.Text, lastNameTextBox.Text);
            }
            catch (Exception e)
            {
                string s = e.Message;
                MessageBox.Show("Не удалось создать нового пользователя");
            }
        }
        private void loginTextBox_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(loginTextBox.Text))
            {
                MessageBox.Show("Поле \"Логин\" должно быть заполнено");
                e.Cancel = true;
            }
        }

        private void txbPassword_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(txbPassword.Text))
            {
                MessageBox.Show("Поле \"Пароль\" должно быть заполнено");
                e.Cancel = true;
            }
        }

        private void textBox2_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("Поле \"Повторите пароль\" должно быть заполнено");
                e.Cancel = true;
            }
        }
    }
}